package com.karry.yl.web.system;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.karry.commons.base.SuperController;
import com.karry.commons.vo.LayTableResponse;
import com.karry.yl.domain.system.Advice;
import com.karry.yl.service.system.AdviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


@Controller
@RequestMapping("/advice")
public class AdviceController extends SuperController {

    @Autowired
    private AdviceService adviceService;


    /**
     * 用户管理列表页
     *
     * @return
     */
    @RequestMapping("/ui/list")
    public String listUI() {
        return "system/advice/list";
    }

    @RequestMapping("/list")
    @ResponseBody
    public LayTableResponse list(int page, int limit) {
        //查询出院内通知，已经上级部门发的通知
        Long orgId = currentUser.getOrgId();
        Page<Advice> advicePage = adviceService.selectAdvice(new Page<Advice>(page, limit), orgId);
       // Page<Advice> advicePage = adviceService.selectPage(new Page<Advice>(page, limit), new EntityWrapper<Advice>().eq("deleted",false).eq("orgId", currentUser.getOrgId()).orderBy("createdTime",false));

        return new LayTableResponse(advicePage.getRecords(), advicePage.getTotal());
    }

    /**
     * @param id
     * @param modelMap
     * @return string
     */
    @RequestMapping("/show")
    public String show(Long id, ModelMap modelMap) {
        Advice advice = adviceService.selectById(id);
        modelMap.put("advice", advice);
        return "system/advice/detail";
    }

    /**
     * @return string
     */
    @RequestMapping("/publish")
    public String publishUI() {
        return "system/advice/add";
    }

    @RequestMapping("/save")
    public String save(Advice advice) {
        advice.setOrgId(currentUser.getOrgId());
        adviceService.insert(advice);
        return "redirect:/advice/ui/list";
    }

    @RequestMapping("/edit")
    public String editUI(Long id, ModelMap map) {
        //根据id查询通知记录
        Advice advice = adviceService.selectById(id);
        map.put("advice", advice);
        return "system/advice/edit";
    }

    @RequestMapping("/update")
    public String update(Advice advice) {
        advice.setOrgId(currentUser.getOrgId());
        adviceService.updateById(advice);
        return "redirect:/advice/ui/list";
    }

    /**
     * 删除
     *
     * @param
     * @return
     */
    @RequestMapping(path = "/delete")
    public String delete(@RequestParam("id") List<Long> ids) {
        adviceService.deleteBatchIds(ids);
        return  "redirect:/advice/ui/list";
    }
}
